package com.zw.a.basic.a11.demo04_exercise;

import com.itheima.pojo.Student;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//Map集合案例: 键是Student, 值是String
//结论: HashMap保证键的唯一性依赖: hashCode(), equals()方法.
public class Demo02 {
    public static void main(String[] args) {
        //1.创建HashMap集合, 键是学生对象(Student), 值是居住地(String).
        HashMap<Student, String> hm = new HashMap<>();
        //2.往HashMap集合中添加3组数据.
        hm.put(new Student("乔峰", 36), "北京");
        hm.put(new Student("虚竹", 29), "上海");
        hm.put(new Student("段誉", 21), "广州");

        //hm.put(new Student("段誉", 21), "深圳");

        //3.通过两种方式, 遍历HashMap集合.
        //方式一: 根据键获取其对应的键和值.
        Set<Student> keys = hm.keySet();
        for (Student key : keys) {
            System.out.println(key + "..." + hm.get(key));
        }
        System.out.println("-------------------------");

        //方式二: 根据键值对获取其对应的键和值.
        Set<Map.Entry<Student, String>> entrys = hm.entrySet();
        for (Map.Entry<Student, String> entry : entrys) {
            System.out.println(entry.getKey() + "..." + entry.getValue());
        }
    }
}
